package com.situ.ymall.mapper;

import com.situ.ymall.entity.GoodsSort;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsSortMapper {
    @Delete({
        "delete from goods_sort",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into goods_sort (name, ",
        "picture, parent)",
        "values (#{name,jdbcType=VARCHAR}, ",
        "#{picture,jdbcType=VARCHAR}, #{parent,jdbcType=INTEGER})"
    })
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int insert(GoodsSort record);

    @Select({
        "select",
        "*",
        "from goods_sort",
        "where id = #{id}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column = "parent" ,property = "parentSort",
                    one = @One(select ="selectByPrimaryKey") )
    })
    GoodsSort selectByPrimaryKey(@Param("id") Integer id);

    @Select({
        "select",
        "id, name, picture, parent, status",
        "from goods_sort"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    List<GoodsSort> selectAll();

    @Update({"<script>",
        "update goods_sort",
        "<set>",
        "<if test='name != null'>name=#{name},</if>",
        "<if test='picture != null'>picture=#{picture},</if>",
        "<if test='parent == null'>parent=null,</if>",
        "<if test='parent != null'>parent=#{parent},</if>",
        "<if test='status != null'>status=#{status},</if>",
        "</set>",
        "where id = #{id,jdbcType=INTEGER}",
        "</script>"
    })
    int updateByPrimaryKey(GoodsSort record);
    
    @Select({"<script>",
        "select * from goods_sort",
        "<where>",
        "<if test='name != null'>name like '%${name}%'</if>",
        "<if test='parent == null'> and parent is null</if>",
        "<if test='parent != null and parent != 0'> and parent = #{parent}</if>",
        "<if test='status != null'> and status = #{status}</if>",
        "</where>",
        "</script>"
    })
    @Results({
    	@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
    	@Result(column = "parent" ,property = "parentSort",
    			one = @One(select ="selectByPrimaryKey") ),
        @Result(column = "id",property = "sonSorts",
                many = @Many(select = "selectByParentId"))
    })
    List<GoodsSort> select(GoodsSort goodsSort);

    /**
     * 根据父分类的ID来查询所有的子分类
     */
    @Select({
            "select * from goods_sort where parent = #{id} and status = 0"
    })
    List<GoodsSort> selectByParentId(@Param("id") Integer id);


}